#include "modbus_register.h"

// 语音设置变量
static uint16_t no_cup_announce_time = 0;    // 无人取杯间隔播报时间
static uint16_t idle_announce_time = 0;      // 空闲间隔播报时间

// 主机设置变量
static uint16_t selling_time[2] = {0};       // 售卖时间(开始-结束)
static uint16_t set_hardness = 0;            // 设定硬度
static uint16_t refriger_temp = 0;           // 冷藏温度

// 制冷机参数
static uint16_t first_air_pump_time = 0;     // 第一次气泵工作时间
static uint16_t material_air_pump_time = 0;  // 打料气泵工作时间
static uint16_t air_pump_delay_time = 0;     // 打料气泵延时工作时间

// 工厂设置变量
static uint16_t alarm_range[2] = {0};        // 报警值区间
static uint16_t voltage_alarm_enable = 0;    // 电压报警使能
static uint16_t cleaning_days = 0;           // 清洗天数

// 设置模块寄存器读取
uint16_t modbus_settings_get(uint16_t address) {
    // 语音设置
    if (address == REG_NO_CUP_ANNOUNCE_TIME) {
        return no_cup_announce_time;
    } else if (address == REG_IDLE_ANNOUNCE_TIME) {
        return idle_announce_time;
    }
    // 主机设置
    else if (address >= REG_SELLING_TIME_BASE && address < REG_SELLING_TIME_BASE + 2) {
        return selling_time[address - REG_SELLING_TIME_BASE];
    } else if (address == REG_SET_HARDNESS) {
        return set_hardness;
    } else if (address == REG_REFRIG_TEMP) {
        return refriger_temp;
    }
    // 制冷机参数
    else if (address == REG_FIRST_AIR_PUMP_TIME) {
        return first_air_pump_time;
    } else if (address == REG_MATERIAL_AIR_PUMP_TIME) {
        return material_air_pump_time;
    } else if (address == REG_AIR_PUMP_DELAY_TIME) {
        return air_pump_delay_time;
    }
    // 工厂设置
    else if (address >= REG_ALARM_RANGE_BASE && address < REG_ALARM_RANGE_BASE + 2) {
        return alarm_range[address - REG_ALARM_RANGE_BASE];
    } else if (address == REG_VOLTAGE_ALARM_EN) {
        return voltage_alarm_enable;
    } else if (address == REG_CLEANING_DAYS) {
        return cleaning_days;
    }
    
    return 0;
}

// 设置模块寄存器写入
void modbus_settings_set(uint16_t address, uint16_t value) {
    // 语音设置
    if (address == REG_NO_CUP_ANNOUNCE_TIME) {
        no_cup_announce_time = value;
    } else if (address == REG_IDLE_ANNOUNCE_TIME) {
        idle_announce_time = value;
    }
    // 主机设置
    else if (address >= REG_SELLING_TIME_BASE && address < REG_SELLING_TIME_BASE + 2) {
        selling_time[address - REG_SELLING_TIME_BASE] = value;
    } else if (address == REG_SET_HARDNESS) {
        set_hardness = value;
    } else if (address == REG_REFRIG_TEMP) {
        refriger_temp = value;
    }
    // 制冷机参数
    else if (address == REG_FIRST_AIR_PUMP_TIME) {
        first_air_pump_time = value;
    } else if (address == REG_MATERIAL_AIR_PUMP_TIME) {
        material_air_pump_time = value;
    } else if (address == REG_AIR_PUMP_DELAY_TIME) {
        air_pump_delay_time = value;
    }
    // 工厂设置
    else if (address >= REG_ALARM_RANGE_BASE && address < REG_ALARM_RANGE_BASE + 2) {
        alarm_range[address - REG_ALARM_RANGE_BASE] = value;
    } else if (address == REG_VOLTAGE_ALARM_EN) {
        voltage_alarm_enable = value;
    } else if (address == REG_CLEANING_DAYS) {
        cleaning_days = value;
    }
}
